using Framework.Core.Process;
using Framework.Core.Process.Tasks;

namespace Framework.Core.Io
{
    internal class RemoteProcessRunner : IProcessRunner
    {
        internal class RemoteProcessResultValidator : IProcessResultValidator
        {
            public bool Validate(ProcessResult processResult)
            {
                return processResult.ExitCode == 0;
            }
        }

        private readonly IProcessRunner processRunner;
        private readonly string remoteMachine;
        private readonly string remoteUser;
        private readonly string remotePassword;

        public RemoteProcessRunner(string remoteMachine, string remoteUser = "", string remotePassword = "")
            : this(new ProcessRunner(new RemoteProcessResultValidator()), remoteMachine, remoteUser, remotePassword)
        {
        }

        internal RemoteProcessRunner(IProcessRunner processRunner, string remoteMachine, string remoteUser, string remotePassword)
        {
            this.processRunner = processRunner;
            this.remoteMachine = remoteMachine;
            this.remoteUser = remoteUser;
            this.remotePassword = remotePassword;
        }

        public ProcessResult Start(ICommand command, int timeout = -1)
        {
            var processResult = processRunner.Start(new PsExec(remoteMachine, remoteUser, remotePassword, command), timeout);

            return processResult;
        }
    }
}